package org.appfuse.dao.hibernate.mappingtype;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/** * 从java对象Boolean到sql中的TINYINT的映射 */
public class BooleanMappingType implements UserType {
	private static final int[] TYPE = { Types.TINYINT };

	public int[] sqlTypes() {
		return TYPE;
	}

	public Class returnedClass() {
		return Boolean.class;
	}

	public boolean equals(Object x, Object y) throws HibernateException {
		if (x.equals(y))
			return true;
		if (x == y)
			return true;
		return false;
	}

	public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
			throws HibernateException, java.sql.SQLException {
		Byte b = (Byte) Hibernate.BYTE.nullSafeGet(rs, names[0]);
		if (b == null)
			return Boolean.FALSE;
		if (b.intValue() != 0)
			return Boolean.TRUE;
		return Boolean.FALSE;
	}

	public void nullSafeSet(PreparedStatement psmt, Object value, int index)
			throws HibernateException, java.sql.SQLException {
		Byte b = (value == null) ? new Byte((byte) 0) : (Byte) value;
		Hibernate.BYTE.nullSafeSet(psmt, b, index);
	}

	public Object deepCopy(Object x) throws HibernateException {
		if (x.equals(Boolean.TRUE))
			return new Boolean(true);
		if (x.equals(Boolean.FALSE))
			return new Boolean(false);
		return null;
	}

	public boolean isMutable() {
		return true;
	}

	public Object assemble(Serializable arg0, Object arg1)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	public Serializable disassemble(Object arg0) throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	public int hashCode(Object arg0) throws HibernateException {
		// TODO Auto-generated method stub
		return 0;
	}

	public Object replace(Object arg0, Object arg1, Object arg2)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}
}